﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CompilationFactures.Objects;
using CompilationFactures.Enums;

namespace CompilationFactures.Controls
{
    public partial class ucArticles : UserControl
    {
        private Facture factureEnCours;

        public ucArticles()
        {
            InitializeComponent();
            cmbTaxeArticle.DataSource = Enum.GetValues(typeof(EnumTaxes));
        }

        private void btnRetour_Click(object sender, EventArgs e)
        {
            (this.Parent as FormulairePrincipale).AfficherArticle(false);
        }

        public void ChargerInterface(Facture facture)
        {
            factureEnCours = facture;

            lblNoFacture.Text = facture.NoFacture.ToString();
            lblTypeFacture.Text = facture.DescriptionType;

            ChargerListeArticles();
        }

        private void ChargerListeArticles()
        {
            lvArticles.Items.Clear();
            foreach (Article art in factureEnCours.ListeArticles)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = art.Item;
                lvItem.SubItems.Add(art.Prix.ToString());
                lvItem.SubItems.Add(art.Quantite.ToString());
                lvItem.SubItems.Add(art.Taxe.ToString());
                lvItem.SubItems.Add(art.SousTotal.ToString());

                lvArticles.Items.Add(lvItem);
            }

            lblMontantSousTotal.Text = string.Format("{0:0.00} $", factureEnCours.SousTotal);
            lblMontantTPS.Text = string.Format("{0:0.00} $", factureEnCours.TPS);
            lblMontantTVQ.Text = string.Format("{0:0.00} $", factureEnCours.TVQ);
            lblMontantTotal.Text = string.Format("{0:0.00} $", factureEnCours.Total);
        }

        public Facture GetFacture()
        {
            return factureEnCours;
        }

        private void lblListeArticle_Click(object sender, EventArgs e)
        {

        }

        private void btnAjouter_Click(object sender, EventArgs e)
        {
            Article art = new Article(txtNomArticle.Text, nudPrix.Value, (int)nudQuantite.Value, (EnumTaxes)Enum.ToObject(typeof(EnumTaxes), Convert.ToInt64(cmbTaxeArticle.SelectedItem)));
            factureEnCours.AjouterArticle(art);
            ChargerListeArticles();
        }

        private void btnModifier_Click(object sender, EventArgs e)
        {
            ChargerListeArticles();
        }

        private void btnSupprimer_Click(object sender, EventArgs e)
        {
            foreach (var itemArticle in lvArticles.SelectedItems)
            {
                factureEnCours.RetirerArticle(((ListViewItem)itemArticle).Text);
            }
            
            ChargerListeArticles();
        }

        private void ucArticles_VisibleChanged(object sender, EventArgs e)
        {
            txtNomArticle.Text = "";
            nudPrix.Value = 0.00m;
            nudQuantite.Value = 1;
            cmbTaxeArticle.SelectedIndex = 0;
        }
    }
}
